



//根据指定日期获取产品种类数量数据
export const leftTopData = (data,selectDate) => {
    console.log(data,selectDate)
    const d = data.filter((item) => item.date === selectDate)
    console.log(d)
    const successdata = d.filter(
        (item) => item.productCode.length === 8
    )
    const halfSuccessdata = d.filter(
        (item) => item.productCode.length !== 8
    )
    console.log(successdata,halfSuccessdata)
    return { successdata:successdata.length, halfSuccessdata:halfSuccessdata.length }
}

//根据指定日期获取月产品种类数量数据
export const leftCenterData = (data,selectDate) => {
    const d = data.filter(
        (item) =>
          item.date.split('-')[1] === selectDate.split('-')[1] &&
          item.date.split('-')[0] === selectDate.split('-')[0]
      )
    const successdata = d.filter(
        (item) => item.productCode.length === 8
    ).reduce((acc, cur) => {
        const existingIndex = acc.findIndex(item => item.productCode === cur.productCode);
        if (existingIndex === -1) {
          acc.push(cur);
        }
        return acc;
      }, [])

    const halfSuccessdata = d.filter(
        (item) => item.productCode.length !== 8
    ).reduce((acc, cur) => {
        const existingIndex = acc.findIndex(item => item.productCode === cur.productCode);
        if (existingIndex === -1) {
          acc.push(cur);
        }
        return acc;
      }, [])
    return { successdata:successdata.length, halfSuccessdata:halfSuccessdata.length }
}

//根据当前月获取产品数量
export const leftBottomDataOrRightBottomData = (data,selectDate) => {
    const d = data.filter(
        (item) =>
          item.date.split('-')[1] === selectDate.split('-')[1] &&
          item.date.split('-')[0] === selectDate.split('-')[0]
      )
      const successdata = d.filter(
        (item) => item.productCode.length === 8
    )
    const halfSuccessdata = d.filter(
        (item) => item.productCode.length !== 8
    )
    return { successdata, halfSuccessdata }
}

//根据指定日期获取产品种类数量数据
export const rigthTopData = (dataArr,selectDate) => {
    dataArr = dataArr.filter((item) => item.date === selectDate)
        const dataMap = {
          FinishedProductsCount: [], // 成品产量
          semiFinishedProductsCount: [], // 半成品 [ semi-finished products ]产量
          yield: [], // 成品率 [ yield ]
          FinishedProductsCode: [] // 成品编码
        }
        // 处理没有半成品的数据，将其没有半成品的生成有半成品
        const resultSemi = []
        const eightLengthProductSet = [] // 用于存储长度为 8 的产品

        // 第一遍遍历：存储长度为 8 的产品，并标记存在对应长度超过 8 的产品
        for (const product of dataArr) {
          resultSemi.push(product)
          if (
            product.productCode.length === 8 &&
            !eightLengthProductSet.includes(product.productCode)
          ) {
            eightLengthProductSet.push(product.productCode)
          }
        }

        console.log(eightLengthProductSet)
        // 第二遍遍历：处理没有对应长度超过 8 位产品的 8 位产品
        for (const productCode of eightLengthProductSet) {
          if (
            !resultSemi.some(
              (item) =>
                item.productCode.startsWith(productCode) &&
                item.productCode.length > 8
            )
          ) {
            resultSemi.splice(
              resultSemi.findIndex((item) => item.productCode === productCode) +
                1,
              0,
              {
                productCode: productCode + '1',
                productionQuantity: 0
              }
            )
          }
        }

        console.log(resultSemi)
        // 成品数量
        dataMap.FinishedProductsCount = resultSemi
          .filter((item) => item.productCode.trim().length === 8)
          .map((item) => item.productionQuantity)

        // 半成品数量
        dataMap.semiFinishedProductsCount = resultSemi
          .filter((item) => item.productCode.trim().length !== 8)
          .map((item) => item.productionQuantity)

        // 成品编码
        dataMap.FinishedProductsCode = resultSemi
          .filter((item) => item.productCode.trim().length === 8)
          .map((item) => item.productCode)

        // 成品比例
        dataMap.yield = dataMap.FinishedProductsCount.map((fpro, index) => {
          const sum =
            parseInt(fpro) +
            (parseInt(dataMap.semiFinishedProductsCount[index]) || 0)
          return (fpro / sum).toFixed(4)
        })

        console.log(dataMap.yield)

        return dataMap
      }

//获取当月成品产量前8
export const rightCenterData = (data, selectDate) => {
    const d = data.filter(
        (item) =>
            item.date.split('-')[1] === selectDate.split('-')[1] &&
            item.date.split('-')[0] === selectDate.split('-')[0]
    ).filter((item) => item.productCode.length === 8)
    const dailyProduction = {}

    d.forEach((item) => {
        const {  productCode, productionQuantity } = item
        const key = `${productCode}`    

        // 如果 key 不存在，则创建新的对象，否则在原有数量上累加
        if (!dailyProduction[key]) {
            dailyProduction[key] = {
                ...item,
                productionQuantity: Number(productionQuantity)
            }
        } else {
            dailyProduction[key].productionQuantity = Number(dailyProduction[key].productionQuantity) + Number(productionQuantity)
        }
    })
    
    // 过滤出成品产量前8的产品
    const successdata = Object.values(dailyProduction)
        .sort((a, b) => b.productionQuantity - a.productionQuantity)
        .slice(0, 8)
        .map(item => ({  name: item.productCode, value: item.productionQuantity }))
    console.log(successdata)
    return successdata;
}

//获取产品成品率
export const centerBottomData = (dataArr,selectDate) => {
    dataArr = dataArr.filter(
        (item) =>
            item.date.split('-')[1] === selectDate.split('-')[1] &&
            item.date.split('-')[0] === selectDate.split('-')[0]
    )
   
        const dataMap = {
          FinishedProductsCount: [], // 成品产量
          semiFinishedProductsCount: [], // 半成品 [ semi-finished products ]产量
          yield: [], // 成品率 [ yield ]
          FinishedProductsCode: [] // 成品编码
        }
        // 处理没有半成品的数据，将其没有半成品的生成有半成品
        const resultSemi = []
        const eightLengthProductSet = [] // 用于存储长度为 8 的产品

        // 第一遍遍历：存储长度为 8 的产品，并标记存在对应长度超过 8 的产品
        for (const product of dataArr) {
          resultSemi.push(product)
          if (
            product.productCode.length === 8 &&
            !eightLengthProductSet.includes(product.productCode)
          ) {
            eightLengthProductSet.push(product.productCode)
          }
        }

        console.log(eightLengthProductSet)
        // 第二遍遍历：处理没有对应长度超过 8 位产品的 8 位产品
        for (const productCode of eightLengthProductSet) {
          if (
            !resultSemi.some(
              (item) =>
                item.productCode.startsWith(productCode) &&
                item.productCode.length > 8
            )
          ) {
            resultSemi.splice(
              resultSemi.findIndex((item) => item.productCode === productCode) +
                1,
              0,
              {
                productCode: productCode + '1',
                productionQuantity: 0
              }
            )
          }
        }

        console.log(resultSemi)
        // 成品数量
        dataMap.FinishedProductsCount = resultSemi
          .filter((item) => item.productCode.trim().length === 8)
          .map((item) => item.productionQuantity)

        // 半成品数量
        dataMap.semiFinishedProductsCount = resultSemi
          .filter((item) => item.productCode.trim().length !== 8)
          .map((item) => item.productionQuantity)

        // 成品编码
        dataMap.FinishedProductsCode = resultSemi
          .filter((item) => item.productCode.trim().length === 8)
          .map((item) => item.productCode)

        // 成品比例
        dataMap.yield = dataMap.FinishedProductsCount.map((fpro, index) => {
          const sum =
            parseInt(fpro) +
            (parseInt(dataMap.semiFinishedProductsCount[index]) || 0)
          return (fpro / sum).toFixed(4)
        })

        console.log(dataMap.yield)

        return dataMap
      }

  

//获取当前日期格式
export const getCurrentDate = () => {
    const date = new Date()
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    return `${year}-${month}-${day}`
}